package com.tech.zhidengxian.websocket;

import com.alibaba.fastjson.JSON;
import com.tech.zhidengxian.model.constant.MessageType;
import com.tech.zhidengxian.model.constant.WSMessageType;
import com.tech.zhidengxian.model.dto.CommentDTO;
import com.tech.zhidengxian.model.dto.MessageDTO;
import com.tech.zhidengxian.model.dto.WebSocketMessageDTO;
import com.tech.zhidengxian.service.api.ChatService;
import com.tech.zhidengxian.service.api.CommentService;
import com.tech.zhidengxian.service.api.MessageService;
import com.tech.zhidengxian.util.TimeUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Component
public class MessageHandlerImpl implements MessageHandler{
    @Resource
    private CommentService commentService;
    @Resource
    private MessageService messageService;
    @Override
    public Map<Long,WebSocketMessageDTO> handlerMessage(WebSocketMessageDTO webSocketMessageDTO) {
        String messageType = webSocketMessageDTO.getMessageType();
        Map<Long,WebSocketMessageDTO> map = null;
        switch (messageType){
            case WSMessageType.CHAT_TYPE:
                map = handlerChatMsg(webSocketMessageDTO);
                break;
            case WSMessageType.COMMENT_TYPE:
                map = handlerCommentMsg(webSocketMessageDTO);
                break;
        }
        return map;
    }

    private Map<Long, WebSocketMessageDTO> handlerCommentMsg(WebSocketMessageDTO webSocketMessageDTO) {
        CommentDTO commentDTO = JSON.parseObject(webSocketMessageDTO.getMessage(), CommentDTO.class);
        commentDTO.setCreateTime(TimeUtil.currentDate());
        commentDTO.setIsDeleted(false);
        CommentDTO dto = commentService.publishComment(commentDTO);
        if (dto!=null){
            Map<Long, WebSocketMessageDTO> map = new HashMap<>();
            webSocketMessageDTO.setMessage(JSON.toJSONString(commentDTO));
            Long notifyUid = commentDTO.getNotifyUid();
            Long uid = commentDTO.getPublisherInfo().getId();
            map.put(uid,webSocketMessageDTO);
            map.put(notifyUid,webSocketMessageDTO);
            return map;
        }
        return null;
    }

    private Map<Long, WebSocketMessageDTO> handlerChatMsg(WebSocketMessageDTO webSocketMessageDTO) {
        MessageDTO messageDTO = JSON.parseObject(webSocketMessageDTO.getMessage(), MessageDTO.class);
        messageDTO.setMessageType(MessageType.text);
        messageService.insertMessage(messageDTO);
        webSocketMessageDTO.setMessage(JSON.toJSONString(messageDTO));
        Map<Long, WebSocketMessageDTO> map = new HashMap<>();
        map.put(messageDTO.getCreatorId(), webSocketMessageDTO);
        map.put(messageDTO.getReceiverId(), webSocketMessageDTO);
//        MessageDTO messageDTO = new MessageDTO()
//        CommentDTO commentDTO = JSON.parseObject(webSocketMessageDTO.getMessage(), CommentDTO.class);
        return map;
    }


}
